import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    aliveRouter: ['Index'],
    idNum: '',
    name: '',
    mobileNum: '',
    baseUrl: '' // 照片域名
  },
  getters: {},
  mutations: {
    setIdNum (state, idNum) {
      state.idNum = idNum
    },
    setName (state, name) {
      state.name = name
    },
    setIdMobileNum (state, mobileNum) {
      state.mobileNum = mobileNum
    },
    setBaseUrl (state, baseUrl) {
      state.baseUrl = baseUrl
    }
  },
  actions: {
    /**
     * 添加keep-alive组件
     * @param commit
     * @param payload - {string} - 组件name
     */
    addAliveRouter ({ commit }, payload) {
      const routers = this.state.aliveRouter
      if (!routers.includes(payload)) {
        routers.push(payload)
      }
      commit('updateAliveRouter', routers)
    },
    /**
     * 删除keep-alive组件
     * @param commit
     * @param payload - {string} - 组件name
     */
    delAliveRouter ({ commit }, payload) {
      const routers = this.state.aliveRouter
      const index = routers.findIndex(v => {
        return v === payload
      })
      if (index >= 0) {
        routers.splice(index, 1)
        commit('updateAliveRouter', routers)
      }
    },
    /**
     * 重置keep-alive组件
     */
    resetAliveRouter ({ commit }) {
      commit('updateAliveRouter', ['Index'])
    }
  },
  modules: {
    user
  }
})

export default store
